package Phase_Sixth_Module_One

class User {
  //  用户姓名
  var name: String = _
  //  用户得分
  var score: Int = _
  //  用户获胜次数
  var uWinNum: Int = _
  //  用户平局次数
  var uDrawNum: Int = _
  //  用户失败次数
  var uFailNum: Int = _


  def showFist(index: Int) = {
    index match {
      case 1 => println(s"你出拳：剪刀")
      case 2 => println(s"你出拳：石头")
      case 3 => println(s"你出拳：布")
    }
  }
}

class Computer {
  //  电脑姓名
  var name: String = _
  //  电脑得分
  var score: Int = _
  //  电脑获胜次数
  var cWinNum: Int = _
  //  电脑平局次数
  var cDrawNum: Int = _
  //  电脑失败次数
  var cFailNum: Int = _

  def showFist(index: Int) = {
    index match {
      case 1 => println(s"${name}出拳：剪刀")
      case 2 => println(s"${name}出拳：石头")
      case 3 => println(s"${name}出拳：布")
    }
  }

}

class Game {
  var U = new User
  var C = new Computer
  var battleTimes: Int = _

  def init() = {
    U.name = "游客"
    U.score = 0
    U.uWinNum = 0
    U.uFailNum = 0
    U.uDrawNum = 0

    C.name = "无名"
    C.score = 0
    C.cWinNum = 0
    C.cFailNum = 0
    C.cDrawNum = 0

    battleTimes = 0
  }

  def chooseComputer(cid: Int) = {
    cid match {
      case 1 => C.name = "刘备"
      case 2 => C.name = "关羽"
      case 3 => C.name = "张飞"
    }
  }

  def begin() = {
    battleTimes += 1
    println("请出拳！1.剪刀 2.石头 3.布")
    //    用户出拳
    var index_u = scala.io.StdIn.readInt()
    while (index_u != 1 && index_u != 2 && index_u != 3) {
      println("你出拳有误，请重新出拳")
      index_u = scala.io.StdIn.readInt()
    }
    U.showFist(index_u)
    //    电脑出拳
    val index_c = scala.util.Random.nextInt(3) + 1
    C.showFist(index_c)
    //   判断输赢并计分
    judge(index_u, index_c)
  }

  def judge(index_u: Int, index_c: Int) = {
    //    用户赢
    if (index_u == 1 && index_c == 3 || index_u == 2 && index_c == 1 || index_u == 3 && index_c == 2) {
      println("结果：恭喜，你赢啦！")
      U.score += 2
      U.uWinNum += 1
      C.cFailNum += 1
      //  和局
    } else if (index_u == index_c) {
      println("结果：和局！下次继续努力！")
      U.score += 1
      U.uDrawNum += 1
      C.score += 1
      C.cDrawNum += 1
    } else {
      println(s"结果：${C.name}赢了！下次继续努力！")
      C.score += 2
      C.cWinNum += 1
      U.uFailNum += 1
    }
  }

  def outPutRes() = {
    println("退出游戏！")
    println("------------------------------------------------------------------------")
    println(s"${C.name} VS ${U.name}")
    println(s"对战次数${battleTimes}次")
    println()
    println()
    println("姓名   等分    胜局    和局    负局")
    println(s"${U.name}    ${U.score}      ${U.uWinNum}       ${U.uDrawNum}       ${U.uFailNum}")
    println(s"${C.name}    ${C.score}      ${C.cWinNum}       ${C.cDrawNum}       ${C.cFailNum}")
    println("------------------------------------------------------------------------")
  }
}

object Homework_Two {

  def main(args: Array[String]): Unit = {
    val G = new Game
    var flag = true
    G.init()
    //初始化游戏
    println("-----------欢迎进入游戏世界-----------")
    println("*************************************")
    println("**************猜拳,开始***************")
    println("*************************************")

    println("请选对战角色：（1.刘备  2.关羽  3.张飞）")
    var cid = scala.io.StdIn.readInt()
    while (cid != 1 && cid != 2 && cid != 3) {
      println("您的选择有误，请重新输入")
      cid = scala.io.StdIn.readInt()
    }
    G.chooseComputer(cid)
    println(s"您选择了与${G.C.name}对战")
    println("要开始么？y/n")
    var select = scala.io.StdIn.readChar()
    while (select != 'Y' && select != 'y' && select != 'N' && select != 'n') {
      println("输入有误，请重新输入！")
      println("要开始么？y/n")
      select = scala.io.StdIn.readChar()
    }
    if ('y'.equals(select) || 'Y'.equals(select)) {
      while (flag) {
        G.begin()
        println("是否开始下一轮（y/n）")
        var select = scala.io.StdIn.readChar()
        while (select != 'Y' && select != 'y' && select != 'N' && select != 'n') {
          println("输入有误，请重新输入！")
          println("是否开始下一轮（y/n）")
          select = scala.io.StdIn.readChar()
        }
        if (select == 'n' || select == 'N') {
          flag = false
        }
      }
    }
    G.outPutRes()

  }
}
